﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.world {
    import flash.events.*;
    import flash.geom.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class CreateRoomCommand extends Command {

		[Inject]
        public var event:InfoEvent;
		[Inject]
        public var worldFactory:WorldFactory;
		[Inject]
        public var worldModel:WorldModel;
		[Inject]
        public var slotsModel:RoomSlotsModel;

        override public function execute():void{
            var _local3:IRoom;
            var _local4:Slot;
            var _local5:Point;
            var _local6:Boolean;
            var _local7:Object;
            var _local8:Boolean;
            var _local1:Object = this.event.info.room;
            var _local2:IArea = this.worldFactory.createArea(_local1);
            this.worldModel.addRoom(_local2);
            dispatch(new InfoEvent(EventConstants.CREATE_ROOM_VIEW, null, _local2));
            if ((_local2 is IRoom)){
                this.checkType(_local1.room_type);
                _local3 = IRoom(_local2);
                _local4 = this.slotsModel.getSlotById(_local3.slotId);
                _local4.setOccupied();
                _local5 = _local4.position.clone();
                dispatch(new InfoEvent(EventConstants.SET_TO_POSITION, _local3.id, _local5));
                _local6 = this.worldModel.isTreatmentRoom(_local1.room_type);
                if (((_local1.pets.length) || (!(_local6)))){
                    dispatch(new InfoEvent(EventConstants.HANDLE_NEXT_IN_QUEUE, _local2.type));
                };
                this.initElements(_local3, ((_local1.elements) || ([])));
                if (_local6){
                    if (!_local1.pets.length){
                        dispatch(new InfoEvent(EventConstants.SHOW_HIRE_DOCTOR_BUTTON, _local1.id));
                    } else {
                        _local7 = _local1.pets[0];
                        _local8 = (_local7[ProtocolConstants.STATE] == WorldConstants.DOCTOR);
                        if (((_local8) && (_local7.needs_welcome))){
                            dispatch(new InfoEvent(EventConstants.SHOW_WELCOME_DOCTOR_BUTTON, _local1.id, _local7));
                        };
                    };
                };
            };
        }
        private function checkType(_arg1:String):void{
            if (_arg1 == GameConstants.TYPE_KIOSK){
                dispatch(new Event(EventConstants.KIOSK_AVAILABLE));
            } else {
                if (_arg1 == GameConstants.TYPE_KINDERGARTEN){
                    dispatch(new Event(EventConstants.KINDERGARTEN_AVAILABLE));
                    dispatch(new Event(EventConstants.REMOVE_KINDERGARTEN_TEASER));
                    dispatch(new InfoEvent(EventConstants.MOVE_TEASER_BABY_TO_KINDERGARTEN, null, this.event.info));
                    dispatch(new Event(EventConstants.REMOVE_KINDERGARTEN_FROM_SHOP_MENU));
                } else {
                    if (_arg1 == GameConstants.TYPE_FOUNTAIN){
                        dispatch(new Event(EventConstants.REMOVE_FOUNTAIN_FROM_SHOP_MENU));
                    };
                };
            };
        }
        private function initElements(_arg1:IRoom, _arg2:Array):void{
            var _local3:Object;
            var _local4:ElementVO;
            for each (_local3 in _arg2) {
                _local4 = this.worldFactory.createElement(_local3);
                _arg1.addElement(_local4);
                dispatch(new InfoEvent(EventConstants.ADD_ELEMENT, _local4.roomId, _local4));
            };
        }

    }
}//package net.wooga.pets.controllers.world 
